home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / SAT 2.1.2 / HeartQuest sample ƒ / scores.p < prev    next >
Encoding:
Text File  |  1994-06-09  |  12.5 KB  |  324 lines  |  [TEXT/PJMM]

  1. g a new game, you will probably}
  2. { need to rewrite this unit a lot. }
  3.  
  4. unit scores;
  5.  
  6. interface
  7.     uses
  8.         transskel, SAT, Preferences, GameGlobals, SoundConst, CenterStuff;
  9.  
  10.     var
  11.         score: longint;
  12.  
  13.     procedure DoHighMenu (item: integer);
  14.     procedure InitScores;                    { Loads the high score list and the high score window. }
  15.     procedure ZeroScore;                     { Call this on New Game! }
  16.     procedure AddScore (amount: longint);    { Call this when the player gets points, or with addscore(0) just to redisplay. }
  17.     procedure AddScoreS (amount: longint);    { Call this to redisplay when the animation isn't running. }
  18.     procedure UpdateHigh;                    { Call this on game over! }
  19.  
  20. implementation
  21.  
  22. { Highscore record }
  23.     type
  24.         hsRec = record
  25.                 HighScores: array[0..10] of longint;
  26.                 HighPlayer: array[0..10] of str15;
  27.             end;
  28.         hsPtr = ^hsRec;
  29.         hsHnd = ^hsPtr;
  30.  
  31.  
  32.     var
  33.         hs, hsm: hsHnd; { m is for macho mode }
  34.         hsh, hshm: Handle;
  35.  
  36. { Bitmaps and rects for drawhex }
  37. {    srcr, erasr: Rect;}
  38. {    bm: BitMap;}
  39. {    icon: Handle;}
  40. {    bma: array[0..9] of BitMap;}
  41. {    icons: array[0..9] of Handle;}
  42.  
  43. {Filter function for AskHigh, ok = 1 and cancel = 4}
  44.     function Filter (theDialog: DialogPtr; var theEvent: EventRecord; var itemHit: integer): boolean;
  45.         var
  46.             theChar: Char;
  47.             kind: integer;
  48.             item: Handle;
  49.             box: Rect;
  50.     begin
  51.         if theEvent.what = keyDown then
  52.             begin
  53.                 theChar := Char(BitAnd(theEvent.message, charCodeMask));
  54.                 if BitAnd(theEvent.modifiers, cmdkey) <> 0 then
  55.                     if theChar = '.' then
  56.                         begin
  57.                             itemHit := 4;
  58. {Highlight the cancel button}
  59.                             GetDItem(theDialog, 4, kind, item, box);
  60.                             HiliteControl(ControlHandle(item), 1);
  61.  
  62.                             Filter := true;
  63.                             exit(Filter);
  64.                         end;
  65.                 if (theChar = char(13)) or (theChar = char(3)) then
  66.                     begin
  67.                         itemHit := 1;
  68. {Highlight the OK button}
  69.                         GetDItem(theDialog, 1, kind, item, box);
  70.                         HiliteControl(ControlHandle(item), 1);
  71.  
  72.                         Filter := true;
  73.                         exit(Filter);
  74.                     end;
  75.             end;
  76.         Filter := false;
  77.     end;
  78.  
  79. {Put a frame around a dialog item. There are better ways to do this, though. The right way}
  80. {is to draw the frame as response to an update event, not just when opening the dialog.}
  81.     procedure FrameDItem (dLog: DialogPtr; iNum: integer);
  82.         var
  83.             iBox: Rect;
  84.             iType: integer;
  85.             iHandle: Handle;
  86.             oldPenState: PenState;
  87.             tmpp: GrafPtr;
  88.     begin
  89.         GetPort(tmpp);
  90.         SetPort(dLog);
  91.         GetPenState(oldPenState);
  92.         GetDItem(dLog, iNum, iType, iHandle, iBox);
  93.         InsetRect(iBox, -4, -4);
  94.         PenSize(3, 3);
  95.         FrameRoundRect(iBox, 16, 16);
  96.         SetPenState(oldPenState);
  97.         SetPort(tmpp);
  98.     end;
  99.  
  100. { Ask for players name (at highscore) }
  101.     function AskHigh: str255;
  102.         var
  103.             dialog: DialogPtr;
  104.             oldPort: GrafPtr;
  105.             dRec: DialogRecord;
  106.             itemHit: integer;
  107.             itemHandle: Handle;
  108.             itemType, item: integer;
  109.             itemRect: Rect;
  110.             str: str255;
  111.             levelstr: str255;
  112.     begin
  113.         CenterDialog(highDlog);
  114.         GetPort(oldPort);
  115.         dialog := GetNewDialog(highDlog, @dRec, WindowPtr(-1));
  116.         ShowWindow(dialog);
  117.         SelectWindow(dialog);
  118.         SetPort(dialog);
  119.  
  120.         GetDItem(dialog, 3, itemType, itemHandle, itemRect);
  121.         SetIText(itemHandle, features^^.player);
  122.         SelIText(dialog, 3, 0, 32767);
  123.         FrameDItem(dialog, 1);
  124.         itemHit := -1;
  125.         while (itemHit <> 1) and (itemHit <> 4) do { 1=ok, 4=cancel }
  126.             ModalDialog(@Filter, itemHit);
  127.         if itemHit = 4 then
  128.             begin
  129.                 AskHigh := '';
  130.             end;
  131.         if itemHit = 1 then
  132.             begin
  133.                 GetDItem(dialog, 3, itemType, itemHandle, itemRect);
  134.                 GetIText(itemHandle, str);
  135.                 if length(str) > 15 then
  136.                     str := Copy(str, 1, 15);
  137.                 features^^.player := str;
  138.                 AskHigh := str;
  139.             end;
  140.         CloseDialog(dialog);
  141.         SetPort(oldPort);
  142.     end;
  143.  
  144. {     High Score window handlers }
  145.  
  146.     procedure HighUpdate (resized: boolean);
  147.         var
  148.             s: str255;
  149.             i: integer;
  150.     begin
  151.         EraseRect(theHigh^.portrect);
  152.         TextSize(9);
  153.  
  154.         moveto(10, 20);
  155.         DrawString(MyGetIndString(normalStrID)); {str 9: Normal high score list}
  156.         MoveTo(150, 20);
  157.         DrawString(MyGetIndString(machoStrID)); {str 10: Macho high score list}
  158.         MoveTo(0, 22);
  159.         LineTo(500, 22);
  160.         MoveTo(140, 0);
  161.         LineTo(140, 400);
  162.  
  163.         for i := 1 to 10 do
  164.             begin
  165.                 if not LastMacho and (i = LastHigh) then
  166.                     begin
  167.                         TextFace([bold]);
  168.                         ForeColor(redColor);
  169.                     end;
  170.                 moveto(10, i * 18 + 20);
  171.                 DrawString(hs^^.HighPlayer[i]);
  172.                 moveto(110, i * 18 + 20);
  173.                 NumToString(hs^^.HighScores[i], s);
  174.                 DrawString(s);
  175.  
  176.                 TextFace([]);
  177.                 ForeColor(BlackColor);
  178.                 if LastMacho and (i = LastHigh) then
  179.                     begin
  180.                         TextFace([bold]);
  181.                         ForeColor(redColor);
  182.                     end;
  183.                 moveto(150, i * 18 + 20);
  184.                 DrawString(hsm^^.HighPlayer[i]);
  185.                 moveto(250, i * 18 + 20);
  186.                 NumToString(hsm^^.HighScores[i], s);
  187.                 DrawString(s);
  188.  
  189.                 TextFace([]);
  190.                 ForeColor(BlackColor);
  191.             end;
  192.         TextSize(12);
  193.     end;
  194.  
  195.     procedure HighHalt;
  196.     begin
  197.         CloseWindow(theHigh);
  198.     end;
  199.  
  200.     function InternalAddScore (amount: longint): Rect;
  201.         var
  202.             s: str255;
  203.             r: Rect;
  204.     begin
  205.         score := score + amount;
  206.  
  207.         SetPort(gSAT.backScreen);
  208.         SetRect(r, gSAT.offSizeH - 49, 14, gSAT.offSizeH - 2, 155);
  209.         EraseRoundRect(r, 10, 10);
  210.         FrameRoundRect(r, 10, 10);
  211.         NumToString(Score, s);
  212.         MoveTo(gSAT.offSizeH - 47, 30);
  213.         DrawString(MyGetIndString(scoreStrID)); {str 11: Score: }
  214.         MoveTo(gSAT.offSizeH - 47, 50);
  215.         DrawString(s);
  216.  
  217.         NumToString(bonus, s);
  218.         MoveTo(gSAT.offSizeH - 47, 80);
  219.         DrawString(MyGetIndString(bonusStrID)); {str 12: Bonus: }
  220.         MoveTo(gSAT.offSizeH - 47, 100);
  221.         DrawString(s);
  222.  
  223.         NumToString(Level, s);
  224.         MoveTo(gSAT.offSizeH - 47, 130);
  225.         DrawString(MyGetIndString(levelStrID)); {str 13: Level: }
  226.         MoveTo(gSAT.offSizeH - 47, 150);
  227.         DrawString(s);
  228.         InternalAddScore := r;
  229.     end;
  230.  
  231.     procedure AddScore (amount: longint);
  232.         var
  233.             s: str255;
  234.             r: Rect;
  235.             tmpport: grafptr;
  236.     begin
  237.         GetPort(tmpPort);
  238.         r := InternalAddScore(amount);
  239.         SATBackChanged(r); {Let SAT show it on screen}
  240.         SetPort(tmpPort);
  241.     end;
  242.  
  243.     procedure AddScoreS (amount: longint);
  244.         var
  245.             s: str255;
  246.             r: Rect;
  247.             tmpport: grafptr;
  248.     begin
  249.         GetPort(tmpPort);
  250.         r := InternalAddScore(amount);
  251.         CopyBits(gSAT.backScreen^.portbits, gSAT.wind^.portBits, r, r, srcCopy, nil);
  252.         CopyBits(gSAT.backScreen^.portbits, gSAT.offScreen^.portBits, r, r, srcCopy, nil);
  253.         SetPort(tmpPort);
  254.     end;
  255.  
  256.     procedure DoHighMenu (item: integer);
  257.         var
  258.             p: procptr;
  259.             i: integer;
  260.     begin
  261.         case item of
  262.             showhs: 
  263.                 begin
  264.                     ShowWindow(theHigh);
  265.                     SelectWindow(theHigh);
  266.                 end;
  267.             clearhs: 
  268.                 begin
  269.                     if QuestionStr(MyGetIndString(sureStrID)) then {str 14: Are you sure you want to erase the high scores?}
  270.                         begin
  271.                             for i := 1 to 10 do
  272.                                 begin
  273.                                     hs^^.HighScores[i] := 0;            { skall läsas från fil eller resurs }
  274.                                     hs^^.HighPlayer[i] := MyGetIndString(nobodyStrID); {str 15: Nobody}
  275.                                     hsm^^.HighScores[i] := 0;            { skall läsas från fil eller resurs }
  276.                                     hsm^^.HighPlayer[i] := MyGetIndString(nobodyStrID); {str 15}
  277.                                 end;
  278.                             hs^^.HighScores[0] := 10000;            { Lowscore }
  279.                             hsm^^.HighScores[0] := 10000;            { Lowscore }
  280.                             ChangedResource(handle(hs));
  281.                             ChangedResource(handle(hsm));
  282.                             HideWindow(theHigh);
  283.                         end;
  284.                 end;
  285.             otherwise
  286.                 ;
  287.         end;
  288.     end;
  289.  
  290.     procedure WindKey (theChar: char; theMods: integer);
  291.     begin
  292.     end;
  293.  
  294. { Call this on game over! }
  295.     procedure UpdateHigh;
  296.         var
  297.             num, len: integer;
  298.             name, s: str255;
  299.     begin
  300.         lastMacho := features^^.macho;
  301.  
  302.         if features^^.macho then
  303.             begin
  304.                 if score > hsm^^.HighScores[10] then
  305.                     begin
  306.                         num := 10;
  307.                         name := AskHigh;
  308.                         NumToString(level, s); {used below, to append level number}
  309. {Max 15 characters! We take some extra trouble to append '…' too.}
  310.                         len := length(stringof(' (', s, ')'));
  311.                         if length(name) > 15 - len then
  312.                             name := Concat(Copy(name, 1, 15 - len - 1), '…');
  313.  
  314.                         if name = '' then { alt length(name) = 0 }
  315.                             exit(updatehigh);
  316.                         while (hsm^^.HighScores[num - 1] < score) and (num > 1) do
  317.                             begin
  318.                                 hsm^^.HighScores[num] := hsm^^.HighScores[num - 1];
  319.                                 hsm^^.HighPlayer[num] := hsm^^.HighPlayer[num - 1];
  320.                                 num := num - 1;
  321.                             end;
  322.                         LastHigh := num; {Remember last high for the highscore display}
  323.                         hsm^^.HighScores[num] := score;
  324.                         hsm^^.HighPlayer[num] :